/*
 * 版权所有 ( c ) 联想集团有限公司 2005-2009 保留所有权利。
 * 项目：	Lenovo Push Service
 * 文件名：	SampleUsage.java
 * 版本:    1.0
 * 描述：	
 * 作者：	刘兵召
 * 日期：	May 10, 2010 
 * 修改历史：
 * 【时间】		【修改者】	【修改内容】
 */
package com.feng.demo.jakartacommons.sanselan;

import java.awt.Dimension;
import java.awt.color.ICC_Profile;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.sanselan.FormatCompliance;
import org.apache.sanselan.ImageFormat;
import org.apache.sanselan.ImageInfo;
import org.apache.sanselan.Sanselan;
import org.apache.sanselan.common.IImageMetadata;

/**
 * Sanselan 是一个纯 Java
 * 的图形库，可以读写各种格式的图像文件，包括快速解析图片信息例如大小/颜色/icc以及元数据等。尽管因为是Java开发的，在处理速度上会稍微慢一些，但具备良好的可移植性。虽然尚未发布1.0
 * 版本，但是已经有多个项目在使用 Sanselan 来处理图像文件。
 * 
 * @author Administrator
 * 
 */
public class SampleUsage {

	public SampleUsage() {

		try {
			// <b>Code won't work unless these variables are properly
			// initialized.
			// Sanselan works equally well with File, byte array or InputStream
			// inputs.</b>
			BufferedImage someImage = null;
			byte someBytes[] = null;
			File someFile = null;
			InputStream someInputStream = null;
			OutputStream someOutputStream = null;

			// <b>The Sanselan class provides a simple interface to the library.
			// </b>

			// <b>how to read an image: </b>
			byte imageBytes[] = someBytes;
			BufferedImage image_1 = Sanselan.getBufferedImage(imageBytes);

			// <b>methods of Sanselan usually accept files, byte arrays, or
			// inputstreams as arguments. </b>
			BufferedImage image_2 = Sanselan.getBufferedImage(imageBytes);
			File file = someFile;
			BufferedImage image_3 = Sanselan.getBufferedImage(file);
			InputStream is = someInputStream;
			BufferedImage image_4 = Sanselan.getBufferedImage(is);

			// <b>Write an image. </b>
			BufferedImage image = someImage;
			File dst = someFile;
			ImageFormat format = ImageFormat.IMAGE_FORMAT_PNG;
			Map optional_params = new HashMap();
			Sanselan.writeImage(image, dst, format, optional_params);

			OutputStream os = someOutputStream;
			Sanselan.writeImage(image, os, format, optional_params);

			// <b>get the image's embedded ICC Profile, if it has one. </b>
			byte iccProfileBytes[] = Sanselan.getICCProfileBytes(imageBytes);

			ICC_Profile iccProfile = Sanselan.getICCProfile(imageBytes);

			// <b>get the image's width and height. </b>
			Dimension d = Sanselan.getImageSize(imageBytes);

			// <b>get all of the image's info (ie. bits per pixel, size,
			// transparency, etc.) </b>
			ImageInfo imageInfo = Sanselan.getImageInfo(imageBytes);

			if (imageInfo.getColorType() == ImageInfo.COLOR_TYPE_GRAYSCALE)
				System.out.println("Grayscale image.");
			if (imageInfo.getHeight() > 1000)
				System.out.println("Large image.");

			// <b>try to guess the image's format. </b>
			ImageFormat image_format = Sanselan.guessFormat(imageBytes);
			image_format.equals(ImageFormat.IMAGE_FORMAT_PNG);

			// <b>get all metadata stored in EXIF format (ie. from JPEG or
			// TIFF). </b>
			// <b>org.w3c.dom.Node node =
			// Sanselan.getMetadataObsolete(imageBytes); </b>
			IImageMetadata metdata = Sanselan.getMetadata(imageBytes);

			// <b>print a dump of information about an image to stdout. </b>
			Sanselan.dumpImageFile(imageBytes);

			// <b>get a summary of format errors. </b>
			FormatCompliance formatCompliance = Sanselan
					.getFormatCompliance(imageBytes);

		} catch (Exception e) {

		}
	}

}
